package tools;

import java.util.ArrayList;
import java.io.Serializable;

public class Player implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7585812154404204070L;

	private static enum State {WAITING, PLAYING, EMPTY_SLOT, FOLD};
	private int id;
	private State state;
	private int totalChips;
	private int newChips;
	private int inGameChips;
	private int bet;
	private Hand hand;
	private boolean smallBlind;
	private boolean bigBlind;
	private boolean dealerButton;
	private boolean isAllIn;
	private ArrayList<Card> exchangedCards;
	private int numberExchangedCards;
	private final int chipsSmallBlind;
	private final int chipsBigBlind;
	private String statusGame="";
	
	public Player(int id, int chipsSmallBlind, int chipsBigBlind, int chips)
	{
		this.id = id;
		this.chipsSmallBlind = chipsSmallBlind;
		this.chipsBigBlind = chipsBigBlind;
		this.newChips = chips;
		setEmptySlot();
	}
	
	public Hand getHand()
	{
		return hand;
	}
	
	public boolean isDealerButton()
	{
		return this.dealerButton;
	}
	
	public int getInGameChips()
	{
		return this.inGameChips;
	}
	
	public int getTotalChips()
	{
		return this.totalChips;
	}
	
	public boolean isSmallBlind()
	{
		return this.smallBlind;
	}	

	public boolean isBigBlind()
	{
		return this.bigBlind;
	}
	
	public void setDealerButton()
	{
		this.dealerButton = true;
		this.smallBlind = false;
		this.bigBlind = false;
	}
	
	public void setSmallBlind()
	{
		this.smallBlind = true;
		this.bigBlind = false;
	}

	public void setBigBlind()
	{
		this.smallBlind = false;
		this.bigBlind = true;
	}

	public void setNoSmallBlind()
	{
		this.smallBlind = false;
	}

	public void setNoBigBlind()
	{
		this.bigBlind = false;
	}
	
	public void setNoFunction()
	{
		this.dealerButton = false;
		this.smallBlind = false;
		this.bigBlind = false;	
	}
	
	public void startGame()
	{
		this.setBet(0);
		this.setInGameChips(0);
		this.setStatusGame("");
	}
	
	public void addToTotalChips(int pot)
	{
		this.totalChips += pot;
	}
	
	private void setInGameChips(int inGameChips)
	{
		this.inGameChips = inGameChips;
	}

	public boolean isPlayer()
	{
		return (this.isWaiting() || this.isPlaying() || this.isFold());
	}
	
	public void takeCards(ArrayList<Card> cards)
	{
		hand = new Hand(cards);
	}

	public ArrayList<Card> returnCards()
	{
		ArrayList<Card> cards = hand.getCardList();
		hand = null;
		return cards;
	}
	
	public ArrayList<Card> returnExchangedCards()
	{
		ArrayList<Card> cards = this.exchangedCards; 
		this.exchangedCards = null;
		return cards;
	}
	
	public void betSmallBlind()
	{
		if (this.totalChips == this.chipsSmallBlind)
		{
			this.allIn();
			this.setStatusGame("All-in: " + this.chipsSmallBlind);
		}
		else
			this.bet(this.chipsSmallBlind);
	}
	
	public void betBigBlind()
	{
		if (this.totalChips == this.chipsBigBlind)
		{
			this.allIn();
			this.setStatusGame("All-in: " + this.chipsBigBlind);
		}
		else
			this.bet(this.chipsBigBlind);
	}
	
	public void setBet(int bet)
	{
		this.bet = bet;
	}
	
	public int getBet()
	{
		return this.bet;
	}
	
	public void bet(int chips)
	{
		this.setBet(chips);
		this.totalChips -= chips;
		this.inGameChips += chips;
	}
	
	public void call(int chips)
	{
		bet(chips);
	}
	
	public void check()
	{
		bet(0);
	}
	
	public void raise(int chips)
	{
		bet(chips);
	}
	
	public void fold()
	{
		/* oznaczenie gracza jako nie grajacego */
		this.setFold();
	}
	
	public void allIn()
	{
		/* oznaczenie gracza jako niebioracego udzialu w licytacji ale w wymianie kart bierze udzial */
		bet(this.totalChips);
		this.setAllIn();
	}
	
	public void setEmptySlot()
	{
		this.state = Player.State.EMPTY_SLOT;
		this.smallBlind = false;
		this.bigBlind = false;
		this.dealerButton = false;
		this.totalChips = 0;		
		this.isAllIn = false;
		this.statusGame = "";
	}
	
	public void setFold()
	{
		this.state = Player.State.FOLD;
	}
	
	public boolean isFold()
	{
		return (this.state == Player.State.FOLD); 
	}
	
	public void setWaiting()
	{
		this.state = Player.State.WAITING;
		this.smallBlind = false;
		this.bigBlind = false;
		this.dealerButton = false;	
		this.isAllIn = false;
		this.statusGame = "";		
	}
	
	public void setPlaying()
	{
		this.state = Player.State.PLAYING;
	}

	public boolean isWaiting()
	{
		return (this.state == Player.State.WAITING);
	}
	
	public boolean isPlaying()
	{
		return (this.state == Player.State.PLAYING);
	}
	
	public boolean isEmptySlot()
	{
		return (this.state == Player.State.EMPTY_SLOT);
	}
	
	public void exchangeCards(ArrayList<Card> cards)
	{
		this.exchangedCards = cards;
		if (this.exchangedCards != null)
			this.numberExchangedCards = this.exchangedCards.size();
		else
			this.numberExchangedCards = 0;
	}
	
	public int getNumberExchangedCards()
	{
		return (this.numberExchangedCards);
	}
	
	public int getID()
	{
		return this.id;
	}
	
	public boolean equals(Object o)
	{
		return ((Player)o).id == this.id;
	}

	public boolean isAllIn()
	{
		return isAllIn;
	}

	public void setAllIn()
	{
		this.isAllIn = true;
	}
	
	public void setAllIn(boolean b)
	{
		this.isAllIn = b;
	}

	public boolean hasTotalChipsToGame()
	{
		return (this.totalChips >= this.chipsSmallBlind);
	}

	public String getStatusGame()
	{
		return statusGame;
	}

	public void setStatusGame(String statusGame)
	{
		this.statusGame = statusGame;
	}
	
	public void swapCards(ArrayList<Card> newCards)
	{
		this.hand.swapCards(this.exchangedCards, newCards);
	}

	void setNewChips()
	{
		this.totalChips = this.newChips;
	}
}
